<?php

namespace app\index\controller;

use app\index\service\CompanyService;
use app\index\service\ExportService;
use app\admin\service\DownsizingService;
use library\Controller;
use think\Db;

/**
 * 减员办理
 * Class Downsizing
 * @package app\index\controller
 */
class Downsizing extends Base
{
    /**
     * 减员列表
     */
    public function index()
    {
    	$where[] = ['d.company_id','eq',$this->company['id']];
    	$where[] = ['d.is_deleted','eq',0];
        $status = $this->request->get('status');
        if ($status){
            $where[] = ['d.status','eq',$status];
        }
		$search =  $this->request->get('search');
		if($search){
			$where[] = ['d.code|i.name|i.id_number','like','%'.$search.'%'];
		}
        $list = Db::name('system_downsizing')
        		->alias('d')
				->join('system_increase i','d.increase_id = i.id')
        		->where($where)
        		->field('d.id,d.code,d.increase_id,d.end_date,d.status,d.create_at,i.name,i.id_number,i.mobile')
				->order('d.id desc')
    			->paginate(10,false,['query'=>request()->param(),'type' => 'page\Page','var_page'  => 'page']);
      	$this ->list = $list; 	
      	 //待审核
        $this ->to_examine = Db::name('system_downsizing')->where(['company_id'=>$this->company['id'],'is_deleted'=>0,'status'=>1])->count(); 
        //未通过
        $this ->to_pass = Db::name('system_downsizing')->where(['company_id'=>$this->company['id'],'is_deleted'=>0,'status'=>3])->count(); 	 		 	
        $this->fetch();
    }


	 /**
     * 减员编辑
     */
    public function edit()
    {
    	if($this->request->isPost()){
    		$data = $this->request->post();
			trim_array($data);
			$downsizing = Db::name('system_downsizing')->where(['company_id'=>$this->company['id'],'id'=>$data['id'],'is_deleted'=>0])->find();
			if(!$downsizing){
				$this->error('暂无员工信息');
			}
			$person = Db::name('system_person')->where(['id'=>$downsizing['person_id'],'company_id'=>$this->company['id']])->find();
			if(!$person || in_array($person['status'], [2,3])){
				$this->error('暂无员工或员工不在职，无法减员');
			}
			Db::name('system_downsizing')->where(['company_id'=>$this->company['id'],'id'=>$data['id'],'is_deleted'=>0])->update(['end_date'=>$data['end_date'],'status'=>1]);
			if(!empty($data['insurance']) && $data['insurance']){
				foreach($data['insurance'] as $k=>$v){
					if(!empty($data['insurance_end_date'])){	    			
	    				if(!empty($data['insurance_end_date'][$k]) && $data['insurance_end_date'][$k]){
	    					$array['end_date'] = $data['insurance_end_date'][$k];
	    				}else{
	    					$array['end_date'] = '';
	    				}
		    		}else{
		    			$array['end_date'] = '';
		    		}
		    		if(!empty($data['insurance_desc'])){	    			
	    				if(!empty($data['insurance_desc'][$k]) && $data['insurance_desc'][$k]){
	    					$array['desc'] = $data['insurance_desc'][$k];
	    				}else{
	    					$array['desc'] = '';
	    				}
		    		}else{
		    			$array['desc'] = '';
		    		}
					Db::name('system_downsizing_insurance')->where(['code'=>$downsizing['code'],'insurance'=>$v])->update($array);	    				
		    	}
			}
			$this->success('努力提交中......', url('index/downsizing/subSuccess'));
    	}else{
    		$id = $this->request->get('id');
			$downsizing = Db::name('system_downsizing')->where(['company_id'=>$this->company['id'],'id'=>$id,'is_deleted'=>0])->find();
			if(!$downsizing){
				$this->redirect('index/publicot/transition', ['info'=>'暂无员工信息','type'=>'3']);
			}
			$increase = Db::name('system_increase')->where(['id'=>$downsizing['increase_id']])->find();
			$person = Db::name('system_person')->where(['id'=>$increase['person_id'],'company_id'=>$this->company['id']])->find();
			if(!$person || in_array($person['status'], [2,3])){
				$this->redirect('index/publicot/transition', ['info'=>'暂无员工或员工不在职，无法减员','type'=>'3']);
			}			
			$insurance = Db::name('system_downsizing_insurance')->where(['code'=>$downsizing['code']])->order('insurance asc')->select();
			$this->increase = $increase;
			$this->downsizing = $downsizing;
			$this->list = $insurance;
			$this->insurances = get_option('insurance');
    	}
        $this->fetch();
    }
    
    
     /**
     * 减员反馈
     */
    public function feedback()
    {
    	if($this->request->isPost()){
    		$data = $this->request->post();
			trim_array($data);
			$downsizing = Db::name('system_downsizing')->where(['company_id'=>$this->company['id'],'id'=>$data['id'],'is_deleted'=>0])->find();
			if(!$downsizing){
				$this->error('暂无员工信息');
			}
			$person = Db::name('system_person')->where(['id'=>$downsizing['person_id'],'company_id'=>$this->company['id']])->find();
			if(!$person || in_array($person['status'], [2,3])){
				$this->error('暂无员工或员工不在职，无法减员');
			}
			$find = Db::name('system_downsizing')->where(['person_id'=>$downsizing['person_id'],'status'=>1,'is_deleted'=>0])->find();
			if($find){
				$this->error('该员工正在办理减员，请勿重复提交');
			}
			$incins = Db::name('system_increase_insurance')->where(['person_id'=>$downsizing['person_id'],'status'=>[1,2],'is_deleted'=>0])->find();
			if($incins){
				$this->error('该员工正在增加险种，无法办理减员');
			}
			$count = Db::name('system_downsizing_insurance')->where(['person_id'=>$downsizing['person_id'],'is_deleted'=>0,'status'=>1])->count();
			if($count){
				$this->error('该员工正在减少险种，无法办理减员');
			}
			Db::name('system_downsizing')->where(['company_id'=>$this->company['id'],'id'=>$data['id'],'is_deleted'=>0])->update(['end_date'=>$data['end_date'],'status'=>1]);
			if(!empty($data['insurance']) && $data['insurance']){
				foreach($data['insurance'] as $k=>$v){
					if(!empty($data['insurance_end_date'])){	    			
	    				if(!empty($data['insurance_end_date'][$k]) && $data['insurance_end_date'][$k]){
	    					$array['end_date'] = $data['insurance_end_date'][$k];
	    				}else{
	    					$array['end_date'] = '';
	    				}
		    		}else{
		    			$array['end_date'] = '';
		    		}
		    		if(!empty($data['insurance_desc'])){	    			
	    				if(!empty($data['insurance_desc'][$k]) && $data['insurance_desc'][$k]){
	    					$array['desc'] = $data['insurance_desc'][$k];
	    				}else{
	    					$array['desc'] = '';
	    				}
		    		}else{
		    			$array['desc'] = '';
		    		}
					Db::name('system_downsizing_insurance')->where(['code'=>$downsizing['code'],'insurance'=>$v])->update($array);	    				
		    	}
			}
			$this->success('努力提交中......', url('index/downsizing/subSuccess'));
    	}else{
    		$id = $this->request->get('id');
			$downsizing = Db::name('system_downsizing')->where(['company_id'=>$this->company['id'],'id'=>$id,'is_deleted'=>0])->find();
			if(!$downsizing){
				$this->redirect('index/publicot/transition', ['info'=>'暂无员工信息','type'=>'3']);
			}
			$increase = Db::name('system_increase')->where(['id'=>$downsizing['increase_id']])->find();
			$person = Db::name('system_person')->where(['id'=>$increase['person_id'],'company_id'=>$this->company['id']])->find();
			if(!$person || in_array($person['status'], [2,3])){
				$this->redirect('index/publicot/transition', ['info'=>'暂无员工或员工不在职，无法减员','type'=>'3']);
			}			
			$insurance = Db::name('system_downsizing_insurance')->where(['code'=>$downsizing['code']])->order('insurance asc')->select();
			$this->increase = $increase;
			$this->downsizing = $downsizing;
			$this->list = $insurance;
			$this->insurances = get_option('insurance');
    	}
        $this->fetch();
    }
	
	
	/**
     * 减员查看
     */
    public function view()
    {
    	$id = $this->request->get('id');
		$downsizing = Db::name('system_downsizing')->where(['company_id'=>$this->company['id'],'id'=>$id,'is_deleted'=>0])->find();
		if(!$downsizing){
			$this->redirect('index/publicot/transition', ['info'=>'暂无员工信息','type'=>'3']);
		}
		$increase = Db::name('system_increase')->where(['id'=>$downsizing['increase_id']])->find();
		$insurance = Db::name('system_downsizing_insurance')->where(['code'=>$downsizing['code']])->order('insurance asc')->select();
		$this->increase = $increase;
		$this->downsizing = $downsizing;
		$this->list = $insurance;
		$this->insurances = get_option('insurance');
        $this->fetch();
    }
	
	/**
     * 减员成功
     */
    public function subSuccess()
	{
		$this->fetch();
	}
	
	
	/**
     * 减员删除
     */
    public function del()
    {
    	$ids = $this->request->post('ids');
		if(!$ids){
			$this->error('请选则数据');
		}
		$ids = explode(',', $ids);
		$count = Db::name('system_downsizing')->where(['id'=>$ids,'status'=>2])->count();
		if($count){
			$this->error('所选项中包含已审核数据，请刷新后重试');
		}
		Db::startTrans();
		$re = Db::name('system_downsizing')->where(['id'=>$ids])->update(['is_deleted'=>1]);
		$codes = Db::name('system_downsizing')->where(['id'=>$ids])->column('code');
        $res = Db::name('system_downsizing_insurance')->where(['code'=>$codes])->update(['is_deleted'=>1]);
		if($re && $res){
			//返还点数或次数			
//			DownsizingService::deduction_back($ids);
			Db::commit();
			$this->success('删除成功');
		}else{
            Db::rollback();
            $this->error('删除失败，请刷新后重试');
        }
    }
	
	
	/**
     * 导出
     */
    public function export()
    {
        $data = $this->request->param();
    	$ids = explode(',', $data['id']);
		$list = ExportService::export_downsizing($ids,$this->company['id']);
    	toexcel($list['filename'],$list['titles'],$list['list']);  
    }
}
